package com.muyu.danmo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;

import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoCommunity;
import com.muyu.danmo.domain.DanmoCommunityComment;
import com.muyu.danmo.service.IDanmoCommunityCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 社区评论Controller
 *
 * @author : kunzong
 * @date : 2024-11-19
 */
@RestController
@RequestMapping("/danmoCommunityComment")
public class DanmoCommunityCommentController {

    @Autowired
    private IDanmoCommunityCommentService danmoCommunityCommentService;

    /**
     * 查询社区评论列表
     *
     * @param danmoCommunityComment 查询条件
     * @return 包含社区评论的结果对象
     */
    @PostMapping("/list")
    public Result list(@RequestBody DanmoCommunityComment danmoCommunityComment) {
        List<DanmoCommunityComment> list = danmoCommunityCommentService.selectDanmoCommunityCommentList(danmoCommunityComment);
        return Result.success(list);
    }

    /**
     * 根据 ID 查询单个帖子评论
     *
     * @param id 帖子 ID
     * @return 返回对应的帖子评论对象
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
        DanmoCommunityComment danmoCommunityComment = new DanmoCommunityComment();
        PageHelper.startPage(danmoCommunityComment.getPageNum(), danmoCommunityComment.getPageSize());
        List<DanmoCommunityComment> comment = danmoCommunityCommentService.queryById(id);
        if (comment.size() == 0) {
            return Result.error("暂无评论");
        }
        PageInfo<DanmoCommunityComment> danmoCommunityPageInfo = new PageInfo<>(comment);
        return Result.success(danmoCommunityPageInfo);
    }

    /**
     * 新增社区评论
     *
     * @param danmoCommunityComment 要新增的社区评论对象
     * @return 操作结果
     */
    @Log(title = "新增社区评论", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody DanmoCommunityComment danmoCommunityComment) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoCommunityComment.setAuthorId(loginUser.getUserid());
        danmoCommunityComment.setCreateBy(String.valueOf(loginUser.getUserid()));
        int result = danmoCommunityCommentService.insert(danmoCommunityComment);
        if (result > 0) {
            return Result.success(1, "评论成功");
        } else if (result == 0) {
            return Result.error("评论不能为空");
        } else {
            return Result.error("评论失败");
        }
//        return result > 0 ? Result.success("评论成功") : Result.error("评论不能为空");
    }

    /**
     * 修改社区评论
     *
     * @param danmoCommunityComment 要更新的社区评论对象
     * @return 操作结果
     */
    @Log(title = "修改社区评论", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result edit(@RequestBody DanmoCommunityComment danmoCommunityComment) {
        int result = danmoCommunityCommentService.update(danmoCommunityComment);
        return result > 0 ? Result.success(1, "更新成功") : Result.error("更新失败");
    }

    /**
     * 删除社区评论
     *
     * @param id 要删除的社区评论 ID
     * @return 操作结果
     */
    @Log(title = "删除社区评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{id}")
    public Result remove(@PathVariable("id") String id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        int result = danmoCommunityCommentService.deleteByIds(id);
        if (result == 0) {
            return Result.error("删除失败");
        } else if (result == 2) {
            return Result.error("该评论不存在");
        } else {
            return Result.success(1, "删除成功");
        }
    }
}